package com.example.module_detail

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.common.entity.CommendEntity
import com.bawei.common.entity.ReplyCommendEntity
import com.example.base.RouterPath
import com.example.common.mvi.BaseMviFragment
import com.example.detail.R
import com.example.detail.databinding.FragmentCommendBinding
import com.example.module_detail.adapter.CommendListAdapter
import com.example.module_detail.intent.DetailViewIntent
import com.example.module_detail.state.DetailUiState
import com.example.module_detail.vm.DetailViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = RouterPath.PATH_FRAGMENT_DETAIL_COMMEND)
class CommendFragment : BaseMviFragment<FragmentCommendBinding>() {
    private val mViewModel:DetailViewModel by activityViewModels()
    private var commendList = mutableListOf<CommendEntity>()
    private var replayCommendList = mutableListOf<ReplyCommendEntity>()
    private lateinit var mCommendAdapter:CommendListAdapter

    @Autowired
    @JvmField
    var itemId:String = ""

    override fun initViews() {
        ARouter.getInstance().inject(this)

        mCommendAdapter = CommendListAdapter(commendList)
        mDataBinding.expListView.setAdapter(mCommendAdapter)

        //本地先模拟回复评论数据
        mockReplayCommendList()
    }
    private fun mockReplayCommendList(){
        replayCommendList.add(ReplyCommendEntity(0, 0, "我来了", "2023-12-20 14:23:21", 0, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "假的真不了", "2023-12-20 14:23:23", 1, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "言之有理", "2023-12-20 14:23:25", 2, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "我觉得太假了，一点儿不真实！！", "2023-12-20 14:23:26", 3, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "啥也不是！！", "2023-12-20 14:23:26", 4, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "太傻了！！", "2023-12-20 14:23:26", 5, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "我觉得太假了，一点儿不真实！！", "2023-12-20 14:23:26", 6, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "给你点赞！！", "2023-12-20 14:23:26", 7, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "努力加油！！", "2023-12-20 14:23:26", 8, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "我觉得太假了，一点儿不真实！！", "2023-12-20 14:23:26", 9, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "加油！！", "2023-12-20 14:23:26", 10, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "一点儿不真实！！", "2023-12-20 14:23:26", 11, "小西", 0, 1, "" ))
        replayCommendList.add(ReplyCommendEntity(0, 0, "牛逼牛逼，太漂亮了！！", "2023-12-20 14:23:26", 12, "小西", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ))
    }
    fun loadCommendListById(id:String){
        itemId = id
        if(commendList.size >0)return

        lifecycleScope.launch {
            mViewModel.channel.send(DetailViewIntent.LoadMovieCommend(1,itemId))
        }
    }
    override suspend fun collectUiState() {
        mViewModel.uiState.collect{
            when(it){
                is DetailUiState.LoadMovieCommendSuccess -> updateCommendList(it.list)
                is DetailUiState.LoadMovieCommendFail -> showToast(it.msg)
                else->{}
            }
        }
    }

    private fun updateCommendList(list: List<CommendEntity>){
        //模拟二级评论数据
        list.forEach{
            randomSettReplayCommend(it)
        }

        commendList.clear()
        commendList.addAll(list)
        mCommendAdapter.notifyDataSetChanged()
    }
    override fun getLayoutId(): Int =R.layout.fragment_commend

    private fun randomSettReplayCommend(entity: CommendEntity){
        //模拟一级评论的用户头像
        entity.userlogo = "https://sf1-cdn-tos.bdxiguastatic.com/img/user-avatar/f4217ff67c16a8daf70e423cccc602fb~300x300.image"
        var randomNum = (Math.random() * 10 + 1).toInt()
        entity.replytotal = randomNum
        //二级评论
        while (randomNum > 0){
            val fromIndex = (replayCommendList.size - 2) % ((Math.random() * 10).toInt() + 1)
            entity.replyList.addAll(replayCommendList.subList(fromIndex, fromIndex + 1))
            randomNum -=1
        }
    }


}